package com.PAT.UnSatisfying;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/11/17/16:55
 * @Description:
 * @Target:
 */
@SuppressWarnings("all")
public class 螺旋矩阵 {
    // 满足条件：m×n 等于 N；m≥n；且 m−n 取所有可能值中的最小值。
    public static void main(String[] args) {


        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        int m = getNumn(N);
        int n = N/m;

        Integer[] arrList = new Integer[N];

        for (int i = 0; i < N; i++) {
            arrList[i] = in.nextInt();
        }

      Arrays.sort(arrList, Collections.reverseOrder());

        int[][] arr = saveNum(arrList, m, n);
        OutMethod(arr,m,n);

    }




    // 打印器
    public static void OutMethod(int arr[][],int m,int n){
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n-1; j++) {
                if (arr[i][j]<10){
                    System.out.print("0"+arr[i][j]+" ");
                }else {
                    System.out.print(arr[i][j]+" ");
                }

            }
            if (arr[i][n-1]<10){
                System.out.print("0"+arr[i][n-1]);
            }else {
                System.out.print(arr[i][n-1]);
            }
            System.out.println();
        }
    }







    // 首先解决m,n的问题,求出m就行了,入参N
    public static  int getNumn(int N){
        int m = Integer.MIN_VALUE;

        int len = (int)Math.sqrt(N)+1; // 使用开平方锁数轴
        for (int i = len; i > 1; i--) {
            if(((double)N)/i==N/i){
                m=i;
                break;
            }
        }

        if(m<N/m){
          m = N/m;
        }

        return m;
    }


    // 存数据的东西
    public static int[][] saveNum(Integer[] arr2,int m,int n){
        int[][] arr = new int[m][n];
        int x1 = 0;  // 与n挂钩
        int y1 = 0; // 与m挂钩
        int x2 = n-1;  // 与n挂钩
        int y2 = m-1; // 与m挂钩
        int index =0;
        // 只能往里面存,而不是打印..对于循环次数,要么每次完了判断
        while (y1<=y2&&x1<=x2) {
            for (int i = x1; i <= x2; i++) {
                if (index<m*n)
                arr[y1][i] = arr2[index++];
            }
            y1++;

            for (int i = y1; i <= y2; i++) {
                if (index<m*n)
                arr[i][x2] = arr2[index++];
            }
            x2--;
            for (int i = x2; i >= x1 ; i--) {
                if (index<m*n)
                arr[y2][i] = arr2[index++];
            }
            y2--;

            for (int i = y2; i >= y1 ; i--) {
                if (index<m*n)
                arr[i][x1] = arr2[index++];
            }
            x1++;

        }

        return arr;

    }


}

